package com.kongchengji.zhanguafou.quartz;
import com.kongchengji.zhanguafou.constant.WxInfo;
import com.kongchengji.zhanguafou.domain.QuartzDO;
import com.kongchengji.zhanguafou.service.ICPService;
import com.kongchengji.zhanguafou.utils.FormatTimeUtil;
import okhttp3.*;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;
import java.io.IOException;

@Component // 此注解必加
@EnableScheduling // 此注解必加
public class ICPScheduleTask implements Job {
    private String siteDomain;
    private String openId;

    @Autowired
    private ICPService icpService;

    /**
     * 备案监控定时任务
     * @param context
     * @throws JobExecutionException
     */
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        try {
            //获取传递过来的quartz对象
            JobDataMap dataMap = context.getJobDetail().getJobDataMap();
            QuartzDO quartz = (QuartzDO) dataMap.get("quartz");
            siteDomain = quartz.getUrl();
            openId = quartz.getOpenId();
            //判断备案是否存在
            boolean isExist = icpService.icpResult(siteDomain);
            if(!isExist){
                sendMessage();
            }
        }
        catch (Exception e) {
          //防止抛出位置异常导致任务结束
          e.printStackTrace();
        }

    }

    /**
     * 将消息发送给用户
     */
    private void sendMessage()  {
        String json = buildMessage();
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8")
                , json);
        Request request = new Request.Builder()
                .url("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+ WxInfo.accessToken.getToken())//请求的url
                .post(requestBody)
                .build();
        OkHttpClient client = new OkHttpClient();
        try {
            client.newCall(request).execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    /**
     * 构造返回消息模板
     * @return 返回构造好的消息
     */
    private String buildMessage() {
        String json = "{" +
                    "           \"touser\":\"" + openId + "\"," +
                    "           \"template_id\":\"TKWRYDnM7quh19vpuDp2rFHGNQXKRotGHnztZJMDdwU\"," +
                    "           \"data\":{" +
                    "                   \"first\": {" +
                    "                       \"value\":\"备案信息告警通知\"," +
                    "                       \"color\":\"#FF0000\"" +
                    "                   }," +
                    "                   \"keyword1\":{" +
                    "                       \"value\":\""+siteDomain+"\"," +
                    "                       \"color\":\"#173177\"" +
                    "                   }," +
                    "                   \"keyword2\": {" +
                    "                       \"value\":\"严重告警\"," +
                    "                       \"color\":\"#173177\"" +
                    "                   }," +
                    "                   \"keyword3\":{" +
                    "                       \"value\":\""+ FormatTimeUtil.getFormatTime()+"\"," +
                    "                       \"color\":\"#173177\"" +
                    "                   }," +
                    "                   \"keyword4\":{" +
                    "                       \"value\":\"您的备案信息没有查询到\"," +
                    "                       \"color\":\"#173177\"" +
                    "                   }," +
                    "                   \"remark\": {" +
                    "                       \"value\":\"请及时处理\"," +
                    "                       \"color\":\"#173177\"" +
                    "                   }" +
                    "           }" +
                    "       }";
        return json;
    }
}
